#import "iTunesController.h"

@implementation iTunesController

- (iTunesApplication *) getiTunes {
	return [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
}

- (iTunesTrack *) getCurrentiTunesTrack {
	iTunesApplication *iTunes = [self getiTunes];
	if (![iTunes isRunning])
		return nil;
	
	return iTunes.currentTrack;
}

- (TrackInfo *) getCurrentTrack {
	iTunesTrack *track  = [self getCurrentiTunesTrack];
	if (![track exists])
		return nil;
	
	return [
		[TrackInfo alloc] 
			initWithName: track.name 
			artist:	      track.artist 
			id:			  track.databaseID
			lyrics:	      track.lyrics
	];
}

- (void) updateCurrentTrackLyrics:(NSString *)lyrics {
	iTunesTrack *track  = [self getCurrentiTunesTrack];
	if ([track exists])
		track.lyrics = lyrics;
}

- (BOOL) isCurrentTrack:(TrackInfo *)track {
	NSInteger currentId = [self getCurrentiTunesTrack].databaseID;
	NSInteger actualId = track.id;
	
	return currentId == actualId;
}

@end
